package org.cupk2.zhzm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.cupk2.zhzm.dao.ZhzmKnowFileDao;
import org.cupk2.zhzm.domain.ZhzmKnowFile;
import org.cupk2.zhzm.service.ZhzmKnowFileServiceInter;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBRule;
import org.cupk2.tools.db.commons.DBRuleList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**知识附件 
 * @author cbtg自动生成  2025-2-4 12:19:26 
 */
@Service
@Slf4j
public class ZhzmKnowFileServiceImpl implements ZhzmKnowFileServiceInter {


    @Autowired
    private ZhzmKnowFileDao zhzmKnowFileDao;



























    @Transactional
    @Override
    public ZhzmKnowFile getZhzmKnowFileById(String id) {
        return zhzmKnowFileDao.findById(id);
    }

    @Override
    public List<ZhzmKnowFile> getZhzmKnowFiles(DataQuery query) {
        return zhzmKnowFileDao.queryData(query.setCount(false)).getData(ZhzmKnowFile.class);
    }



    @Transactional
    @Override
    public DataResult searchZhzmKnowFile(DataQuery query) {
        DataResult result = zhzmKnowFileDao.queryData(query);
        return result;
    }

    @Override
    public int getZhzmKnowFileNum(DataQuery query) {
        return zhzmKnowFileDao.countData(query);
    }


    @Transactional
    @Override
    public void delZhzmKnowFile(String id) {
        /*[tree：树形结构使用]
        if ( zhzmKnowFileDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        zhzmKnowFileDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  zhzmKnowFileDao.countData(query);
    }

    @Override
    public int getNumByKnowId(String knowid) {
        return zhzmKnowFileDao.countData(DBRuleList.getInstance().add(new DBRule("KNOWID", knowid, "=")).toList());
    }
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            ZhzmKnowFile node = getZhzmKnowFileById(sourceId);
            if (!"NONE".equals(targetId)) {
                ZhzmKnowFile target = getZhzmKnowFileById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            zhzmKnowFileDao.update(node);

            List<ZhzmKnowFile> list = zhzmKnowFileDao.findSubNodes(sourceId);
            for (ZhzmKnowFile treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        ZhzmKnowFile node = zhzmKnowFileDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(zhzmKnowFileDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        zhzmKnowFileDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            ZhzmKnowFile node =  zhzmKnowFileDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            zhzmKnowFileDao.update(node);
        }
    }*/
}
